﻿Public Class ModuleController
    Private Shared _Instance As ModuleController
    Public Shared ReadOnly Property Instance As ModuleController
        Get
            If (_Instance Is Nothing) Then
                _Instance = New ModuleController
            End If
            Return _Instance
        End Get
    End Property
    'Public _internaltimer As System.Threading.Timer
    'Public _internelTimer As System.Windows.Forms.Timer
    'Red Gate
    Public _internalTimer As System.Timers.Timer

    Private CountAlumnos As Integer = 0
    Private AsyncContext As System.Threading.SynchronizationContext
    Public Event OnChangeAlumnos As EventHandler(Of EventArgs)


    Public Sub Run()
        Me.AsyncContext = System.Threading.SynchronizationContext.Current

        CountAlumnos = SCSModel.Instance.Alumnos.Count()

        Me._internalTimer = New System.Timers.Timer(5000)
        AddHandler Me._internalTimer.Elapsed, AddressOf OnTimer_Elapsed
        Me._internalTimer.Start()
        'Dim dis = New ModuleController
        'Dim dis2 As WeakReference = New WeakReference(dis)
        'dis = Nothing
        'dis2.Target.Run()
    End Sub
    Public Sub OnTimer_Elapsed(ByVal sender As Object, _
                               ByVal e As System.Timers.ElapsedEventArgs)

        Me._internalTimer.Stop()
        Try
            Dim bc As New BusinessLogic.BCSchool
            Dim tr = bc.SearchAlumnos()
            If (tr.Count > Me.CountAlumnos) Then
                Me.CountAlumnos = tr.Count
                Dim alum = tr.First
                Me.AsyncContext.Send(AddressOf UpdateTextShell, alum)
            End If
        Catch ex As Exception
            Throw
        End Try
        Me._internalTimer.Start()
    End Sub
    Sub UpdateTextShell(ByVal sender As Object)
        Dim alum As BusinessEntities.Alumno = sender
        For Each fmr In Application.OpenForms
            If (TypeOf (fmr) Is Shell) Then
                CType(fmr, Shell).TextBox1.Text = alum.nombre + " " + DateTime.Now.ToLongTimeString()
            End If
        Next

    End Sub


    Public Sub RaiseChangeAlunnos(ByVal origen As Object)
        RaiseEvent OnChangeAlumnos(origen, New EventArgs)
    End Sub

End Class
